Skip to content

testing/ostest: split the fork test into task_fork, vfork and fork - #3673

Open
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:fork-semantics-ostest
Open

testing/ostest: split the fork test into task_fork, vfork and fork#3673
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:fork-semantics-ostest

Conversation

@casaroli

@casaroli casaroli commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Summary

This is the apps half of apache/nuttx#19540, and it must merge first. Companion PR: apache/nuttx#19562, whose CI cannot go green until this one lands, because NuttX PRs build against apps master and three things here call fork() unconditionally.

NuttX implements fork() and vfork() as the same function, and is gaining three separate primitives: task_fork() (shares memory, private stack copy, both running), vfork() (shares memory, parent suspended until _exit()/exec()) and POSIX fork() (child gets its own copy). This PR works against NuttX with or without that change, so no test coverage is lost across the transition.

ostest's "vfork" test was never testing vfork(). It has the child write a global and the parent observe the write — the defining property of sharing, not of vfork(), whose defining property is that the parent is suspended and whose contract forbids the child to write anything at all. It is renamed to task_fork.c, unchanged, because that is the primitive it has always described. It is also the clearest single piece of evidence for the proposal: the test upstream has run for years is a task_fork() test wearing vfork()'s name.

vfork.c is rewritten to test what vfork() promises. The child does only what POSIX permits — it calls _exit(42) and nothing else, not even exit(), which would run atexit handlers and flush stdio in the parent's address space. Since the child may not write memory and the parent cannot run while the child lives, the observable is the child's exit status: had the parent not been suspended, it would have reached waitpid() while the child was still alive. Where child status is not retained — ostest_main() sets SA_NOCLDWAIT for the whole run, deliberately — ECHILD is accepted as equally good evidence, since it says the child was already gone when the parent asked.

fork.c is new and tests POSIX fork(): the child's writes to .data, .bss and the heap are invisible to the parent and vice versa, a pointer to a stack local taken before the fork names the same object in both, and the child does everything a vfork() child may not — calls malloc() and printf(), and returns from the function that called fork().

All three run at the top of user_main(). They exercise the lowest-level machinery in the suite — address environments, stack setup, the architecture's register context — so a fault in one takes the process down instead of reporting a failure. Learning that in seconds rather than after everything else has passed matters when a port is being brought up.

The other in-tree callers are audited for which primitive they actually meant:

  • testing/drivers/nand_sim wants a daemon that outlives its caller and shares its memory — task_fork().
  • interpreters/python's _posixsubprocess and netutils/libwebsockets' LWS_HAVE_WORKING_VFORK want the fork-then-exec path — vfork().
  • python's os.fork() and libwebsockets' LWS_HAVE_FORK mean real fork() and stay on CONFIG_ARCH_HAVE_FORK, so they become absent rather than silently wrong.
  • testing/fs/fdsantest's vfork case follows vfork().

Two third-party suites need their source lists narrowed, because they call fork() from code compiled unconditionally:

  • system/libuvtest-fork.c and test-pipe-close-stdout-read-stdin.c are filtered out of the test-*.c glob. Every test they define is already excluded from the task list on NuttX by 0001-libuv-port-for-nuttx.patch, so they were dead code compiled only because fork() happened to be declared. Nothing is lost.
  • testing/ltp — the open_posix_testsuite is filtered through LTP's existing BLACKWORDS mechanism, which already drops tests for absent features and is already conditioned on configuration symbols. Where fork() is not provided this drops 278 of 1943 test files; the pattern [^v_]fork( spares vfork() and task_fork(). Where fork() is provided — everywhere, today — nothing is dropped, so it is a no-op against current master.

That 278-file loss is the honest price of the change: those tests exercise fork(), and on a target without fork() they cannot link. They return per architecture as real fork() lands.

Two other fork() mentions need nothing: games/NXDoom's is inside #if 0 /* UNUSED */, and system/syslogd already uses posix_spawn().

interpreters/bas is deliberately left alone. Its SHELL and EDIT statements want the same treatment as the others, but checkpatch.sh checks the whole of any file a patch touches, and bas_statement.c produces 1681 pre-existing findings before this patch is applied at all — a one-newline commit against master fails CI identically. Migrating BAS has to follow a style cleanup of that file, and neither belongs here. The consequence is small: CONFIG_EXAMPLES_BAS_SHELL is EXPERIMENTAL and already depends on ARCH_HAVE_FORK, so it becomes unselectable rather than misbehaving.

Impact

Against today's NuttX, ostest builds and runs exactly the fork test it runs now. task_fork.c is that test, byte for byte, under the name of the primitive it describes. A NuttX without CONFIG_ARCH_HAVE_TASK_FORK is the pre-split one, and only there does CONFIG_ARCH_HAVE_FORK stand in, with task_fork() mapped to fork().

vfork_test() and fork_test() have no such fallback, deliberately. Both check semantics a pre-split NuttX does not describe — the parent suspension and the private copy — so mapping them onto CONFIG_ARCH_HAVE_FORK would add tests to configurations that never had one. That is not free: vfork.c costs about 470 bytes of .text on armv7-m at -Os, which put lm3s6965-ek:qemu-protected over its 128 KiB user flash region. They activate on the symbol that announces the primitive, CONFIG_ARCH_HAVE_VFORK.

task_fork.c keys on CONFIG_TASK_FORK, not on the capability symbol. On the NuttX side ARCH_HAVE_TASK_FORK says the architecture can clone a task while TASK_FORK says the build asked for it, and task_fork() is only declared under the latter. Gating the test on the capability alone would fail to compile a TASK_FORK=n build. CONFIG_TESTING_NAND_SIM gains the same dependency; it called fork() unconditionally before and would not have linked on a target without it.

The whole compatibility layer is one #if pair and a task_fork() -> fork() shim in ostest.h, plus the matching build-file conditions. A small follow-up removes it once the NuttX side is in; that follow-up must not merge before the NuttX PR.

Testing

Host: macOS 15 (Darwin 25.5.0) on Apple Silicon. QEMU 11.0.3, xPack riscv-none-elf-gcc 14.2.0-3, Arm GNU arm-none-eabi-gcc 14.2.Rel1.

Against unmodified NuttX master (5a7f1b5005) — the case this PR must not break

rv-virt:nsh64, ostest: config has only CONFIG_ARCH_HAVE_FORK=y, as expected — neither new symbol exists. nm on the image shows task_fork_test built, vfork_test and fork_test absent — exactly the intent. Run: task_fork_test: Child 5 ran successfully, ostest_main: Exiting with status 0.

lm3s6965-ek:qemu-protected, the size-constrained configuration this PR must not overflow: builds clean, 596 bytes free of the 128 KiB uflash region against a 708-byte baseline on apps master.

The LTP filter, verified

Built rv-virt:citest (the CI config that enables LTP) against the NuttX PR branch and mapped every object back to its source:

fork-using LTP sources that produced an object: 0 (of 278)
other LTP objects built:                        896
implicit-declaration errors for fork():         0

Against the NuttX PR branch

Full ostest suite to exit status 0 on rv-virt:nsh64 (FLAT), rv-virt:pnsh64 (PROTECTED), rv-virt:knsh64 (KERNEL), qemu-armv7a:nsh, qemu-armv8a:nsh and qemu-intel64:nsh, with task_fork_test and vfork_test passing and fork_test correctly absent — no architecture provides POSIX fork() at that point in the series. sim:ostest also builds and passes both.

fork_test() itself is verified by the per-architecture PRs that follow, which are what turn CONFIG_ARCH_HAVE_FORK back on. It has been run to completion on RISC-V, arm64, armv7-a and x86_64 kernel builds on the development branch those PRs are cut from — fork_test: Parent and child had independent memory — so it is not being added untested; it is simply not reachable until the first up_addrenv_fork() lands.

Style

../nuttx/tools/checkpatch.sh -c -u -m -g <base>..HEAD, the exact command .github/workflows/check.yml runs — ✔️ All checks pass, with codespell, cvt2utf, cmake-format and nxstyle all installed.

@jerpelea jerpelea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please replace
Co-Authored-By: Claude Opus 5 (1M context) noreply@anthropic.com
with
Assisted-by: Claude Opus 5 (1M context) noreply@anthropic.com

@casaroli
casaroli force-pushed the fork-semantics-ostest branch from e32964b to 536349e Compare July 28, 2026 07:53
nuttx implements fork() and vfork() as the same function, and is gaining the
three separate primitives its issue #19540 describes:  task_fork() (shares
memory, private stack copy, both running), vfork() (shares memory, parent
suspended) and POSIX fork() (child gets its own copy).  This is the apps side
of that, and it lands first:  it works against nuttx with or without the
split, so the tests keep running across the transition rather than silently
compiling out.

ostest's "vfork" test was never testing vfork().  It has the child write a
global and the parent observe the write -- which is the defining property of
*sharing*, not of vfork(), whose defining property is that the parent is
suspended and whose contract forbids the child to write anything at all.  It
is renamed to task_fork.c, unchanged, because that is the primitive it has
always described.

vfork.c is rewritten to test what vfork() promises.  The child does only what
POSIX permits -- it calls _exit(42), and nothing else, not even exit(), which
would run atexit handlers and flush stdio in the parent's address space.  The
observable is therefore the child's exit status rather than a memory write.
Where child status is not retained -- ostest_main() sets SA_NOCLDWAIT for the
whole run, deliberately -- waitpid() returning ECHILD is accepted as equally
good evidence:  it says the child was already gone when the parent asked.

fork.c is new and tests POSIX fork():  the child's writes to .data, .bss and
the heap are invisible to the parent and vice versa, a pointer to a stack
local taken before the fork names the same object in both, and the child does
everything a vfork() child may not -- calls malloc() and printf(), and returns
from the function that called fork().

All three run at the top of user_main() rather than in the middle.  They
exercise the lowest-level machinery in the suite -- address environments,
stack setup, the architecture's register context -- so a fault in one takes
the process down instead of reporting a failure, and finding that out in
seconds rather than after everything else has passed is the difference
between a usable iteration and a coffee break when a port is being brought
up.

The other in-tree callers are audited for which primitive they actually
meant.  nand_sim wants a daemon that outlives its caller and shares its
memory, which is task_fork().  bas's SHELL and EDIT statements, python's
_posixsubprocess and libwebsockets' feature macros want the fork-then-exec
path, which vfork() serves; python's os.fork() and libwebsockets'
LWS_HAVE_FORK stay on fork() proper.  fdsantest's vfork case follows vfork().

Two third-party suites need their source lists narrowed, because they call
fork() from code that is compiled unconditionally:

* system/libuv -- test-fork.c and test-pipe-close-stdout-read-stdin.c are
  filtered out of the test-*.c glob.  Every test they define is already
  excluded from the task list on NuttX by 0001-libuv-port-for-nuttx.patch --
  the nine fork_* entries and pipe_close_stdout_read_stdin -- so they were
  dead code being compiled only because fork() happened to be declared.
* testing/ltp -- the open_posix_testsuite is filtered through the existing
  BLACKWORDS mechanism, which already drops tests for absent features and is
  already conditioned on configuration symbols.  Where fork() is not
  provided this drops 278 of 1943 test files; the pattern is written to spare
  vfork() and task_fork(), which remain available.  Where fork() is provided
  -- which today is everywhere -- nothing is dropped.

Compatibility: the nuttx symbols this keys on do not exist yet.  task_fork.c
is built where CONFIG_TASK_FORK says task_fork() was built and, on a nuttx that
has no CONFIG_ARCH_HAVE_TASK_FORK at all -- which is the pre-split one -- where
CONFIG_ARCH_HAVE_FORK does.  Today's fork() *is* task_fork(), so the test that
has always covered that primitive keeps running, under its own name, and no
coverage is lost across the transition.  Both spellings are needed because
CONFIG_TASK_FORK is optional on the nuttx side:  ARCH_HAVE_TASK_FORK says the
architecture can clone a task, TASK_FORK says this build asked for it.  A
follow-up removes the fallback once the split has landed.

vfork_test() and fork_test() deliberately have no such fallback.  Both check
semantics a pre-split nuttx does not describe -- the parent suspension and the
private copy -- and ARCH_HAVE_VFORK is the evidence that the split has landed.
Mapping them onto ARCH_HAVE_FORK would also add a test to configurations that
never had one, which is not free:  vfork.c costs about 470 bytes of .text on
armv7-m at -Os, and that is what put lm3s6965-ek:qemu-protected over its 128
KiB user flash region.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>

Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
@casaroli
casaroli force-pushed the fork-semantics-ostest branch from 536349e to 2a694fe Compare July 30, 2026 09:40
@casaroli
casaroli requested a review from jerpelea July 30, 2026 09:59
@casaroli
casaroli marked this pull request as ready for review July 30, 2026 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants